
import * as echarts from '../../../components/ec-canvas/echarts';
let chart = null;
function initChart(canvas, width, height, dpr) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);
  let setLabel = (data) => {
    let opts = [];
    for (let i = 0; i < data.length; i++) {
        let item = {};
        item.name = data[i].name;
        item.value = data[i].value;
        item.label = {
            normal: {
              textStyle: {
                color: color[i]  // 改变标示文字的颜色
            },
                //控制引导线上文字颜色和位置,此处a是显示文字区域，b做一个小圆圈在引导线尾部显示
                show: true,
                //a和b来识别不同的文字区域
                formatter: [
                  '{a|{d}% }',
                  '{b|}' 
              ].join('\n'), //用\n来换行
                rich: {
                    a: {
                        left: 20,
                        padding: [0, 0, -20, 0]
                    },
                    b: {
                        height: 6,
                        width: 6,
                        lineHeight: 0,
                        marginBottom: 10,
                        padding: [0, -6],
                        borderRadius: 6,
                        backgroundColor: color[i], // 圆点颜色和饼图块状颜色一致
                    }
                },
            }
        }
        opts.push(item)
    }
    return opts;
}
let data = [
  {value: 7, name: '7%'},
  {value: 18, name: '18%'},
  {value: 25, name: '25%'},
  {value: 30, name: '30%'},
  {value: 20, name: '20%'},
];
let color =  ['#B7D7FA', '#FFB4CE', '#FFA733', '#FFD41A', '#EAB7FA'];
  var option = {
    color:color,
    series: [
      {
        name: '面积模式',
        type: 'pie',
        radius: [60, 100],
        center: ['50%', '50%'],
        avoidLabelOverlap: false,
        labelLine: {
            normal: {
                show: true,
                length: 10, // 第一段线 长度
                length2: 10, // 第二段线 长度
                align: 'center'
            },
            emphasis: {show: true }
        },
        roseType: 'area',
        data:setLabel(data)
    }
    ]
  };
  chart.setOption(option);
  return chart;
}
var timer = null;
const app = getApp();
import {orderSure,orderCreate} from '../../../api/scanCode.js';
Page({
  data: {
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': ''
    },
    showType:'nodata',
    comUrl:'https://shop.target-gd.com/uploads/attach/wxa/',
    // 进度条百分比
    percent:35,
    // 统计图表
    ec: {
      onInit: initChart
    },
    windowHeight:0,
    price:0,
    orderKey:'',
    proImage:'',
    store_name:'',
    goods_img:''
    // end
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opt) {
    const res = wx.getSystemInfoSync();
    this.setData({ windowHeight:res.windowHeight});
    if(opt.cartId){
      this.sureOrder(opt.cartId);
    }
    if(opt.type){
      this.setData({showType:opt.type });
      if(opt.type == 'load'){
        var num = 35;
        timer = setInterval(() => {
          num += this.randomNum(36,50);
          this.setData({percent:num });
          if(this.data.percent>=100){
            clearInterval(timer);
            this.setData({
              percent:100,
              showType:'finish'
            });
          }
        }, 1000);
      }
    }
  },
  // 生成随机数
  randomNum(min, max) {
    return Math.floor(Math.random() * (max - min)) + min   
  },
   // 确认订单
  sureOrder(cartId){
    orderSure({cartId:cartId}).then(res => {
      this.setData({
          price:res.data.cartInfo.length>0?res.data.cartInfo[0].productInfo['price']:0,
          orderKey:res.data.orderKey,
          proImage:res.data.cartInfo.length>0?res.data.cartInfo[0].productInfo['image_first']:'',
          store_name:res.data.cartInfo.length>0?res.data.cartInfo[0].productInfo['store_name']:'',
          goods_img:res.data.cartInfo.length>0?res.data.cartInfo[0].productInfo['image']:''
        });
    }).catch(res => {
      app.Tips({title: res});
    });
  },
  // 生成订单
  creatOrder(){
    orderCreate(this.data.orderKey).then(res => {
      let {jsConfig,key,orderId} = res.data['result'];
      wx.requestPayment({
        timeStamp:jsConfig.timestamp,
        nonceStr: jsConfig.nonceStr,
        package: jsConfig.package,
        signType: jsConfig.signType,
        paySign: jsConfig.paySign,
        success (res) { 
          wx.navigateTo({
            url: `../report/index?orderid=${orderId}`,
          });
        },
        fail (res) {
          app.Tips({title: '取消支付'});
          wx.reLaunch({url: '../home/index'});
         }
      });
    }).catch(res => {
      app.Tips({title: res});
    });
  },
  mustClick(){
    app.Tips({title: '~现在就帮你去找 稍等一下喔~'});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})